package cn.easybuy.Dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BaseDao {
	protected Connection conn;
	protected Statement stmt;
	protected ResultSet rs;
	protected PreparedStatement pstm;

	public void getConn() {
		String url = "jdbc:mysql://localhost:3306/easybuy";
		String name = "root";
		String pwd = "123456";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, name, pwd);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public int executeSql(String sql, Object... obj) throws Exception {

		try {
			getConn();
			pstm = conn.prepareStatement(sql);
			for (int i = 0; i < obj.length; i++) {
				pstm.setObject(i + 1, obj[i]);
			}
			int count = pstm.executeUpdate();
			return count;
		} finally {
			closeAll();
		}
	}

	public void closeAll() {
		try {
			if (rs != null)
				rs.close();
			// if(stmt!=null) stmt.close();
			if (pstm != null)
				pstm.close();
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
